package com.luyue.zagj.utils;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.luyue.zagj.MainActivity;
import com.luyue.zagj.R;

import static com.zhy.autolayout.utils.ScreenUtils.getStatusBarHeight;

/**
 * Created by Chen qin lang on 2016/12/19.
 * 邮箱 :   619766577@qq.com
 */
public class Windowutil {
    public static void clearWindow(MainActivity activity,int color){
    Window window =activity.getWindow();
    ViewGroup mContentView = (ViewGroup)activity.findViewById(Window.ID_ANDROID_CONTENT);

    //First translucent status bar.
    window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    int statusBarHeight = getStatusBarHeight(activity);

    View mChildView = mContentView.getChildAt(0);
    if (mChildView != null) {
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
        //如果已经为 ChildView 设置过了 marginTop, 再次调用时直接跳过
        if (lp != null && lp.topMargin < statusBarHeight && lp.height != statusBarHeight) {
            //不预留系统空间
            ViewCompat.setFitsSystemWindows(mChildView, false);
            lp.topMargin += statusBarHeight;
            mChildView.setLayoutParams(lp);
        }
    }

    View statusBarView = mContentView.getChildAt(0);

    if (statusBarView != null && statusBarView.getLayoutParams() != null && statusBarView.getLayoutParams().height == statusBarHeight) {
        //避免重复调用时多次添加 View
        statusBarView.setBackgroundColor(activity.getResources().getColor(color));
        return;
    }
    statusBarView = new View(activity);
    ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight);
    statusBarView.setBackgroundColor(activity.getResources().getColor(color));
//向 ContentView 中添加假 View
    mContentView.addView(statusBarView, 0, lp);
}


}
